package com.battcn.platform.service.webmagic.pipeline;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.battcn.platform.entity.crow.Comment;
import com.battcn.platform.entity.crow.CommentList;
import com.battcn.platform.entity.crow.TitleWord;
import com.battcn.platform.entity.crow.movie.Actor;
import com.battcn.platform.entity.crow.movie.Director;
import com.battcn.platform.entity.crow.movie.Movie;
import com.battcn.platform.mapper.crow.movie.ActorMapper;
import com.battcn.platform.mapper.crow.movie.DirectorMapper;
import com.battcn.platform.mapper.crow.movie.MovieMapper;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * Created by yanglei
 *  on 18/01/27
 */
@Component("MoviesSpiderPipeline")
public class MoviesSpiderPipeline implements Pipeline{


    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private ActorMapper actorMapper;
    @Autowired
    private DirectorMapper directorMapper;
   

    @Override
    public void process(ResultItems resultItems, Task task) {

        for(Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {

            if(entry.getKey().equals("actorObj")) {
            	Actor actor = (Actor) entry.getValue();
                if(actor != null) {
                	actorMapper.insert(actor);
                }
            }
            if(entry.getKey().equals("directorObj")) {
            	Director director = (Director) entry.getValue();
            	 if(director != null) {
            		 directorMapper.insert(director);
                 }
            }
            if(entry.getKey().equals("movieObj")) {
            	Movie movie = (Movie) entry.getValue();
            	if(movie != null) {
            		movieMapper.insert(movie);
                }
            }
            
        }

    }
}
